CSHARP-4935: Support casting from an interface to a type that implements that interface in LINQ queries#1419
Conversation
rstam
left a comment
There was a problem hiding this comment.
The LINQ changes in this PR look fine to me.
The concern we had that has kept this on the back burner is whether the change to DiscriminatedInterfaceSerializer would be backward breaking in any way.
| _interfaceType = typeof(TInterface); | ||
| _discriminatorConvention = discriminatorConvention ?? BsonSerializer.LookupDiscriminatorConvention(typeof(TInterface)); | ||
| _objectSerializer = BsonSerializer.LookupSerializer<object>(); | ||
| _objectSerializer = objectSerializer ?? new ObjectSerializer(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type)); |
There was a problem hiding this comment.
Do you see any backward compatibility issues with this change?
There was a problem hiding this comment.
It might be a problem if someone registered their own object serializer for some reasons. Can we implement method like a WithAllowedTypes on objectSerializer? So we lookup the same way as it was before - and then try to use this new method, similar to this:
var objectSerializer = BsonSerializer.LookupSerializer<object>();
if (objectSerializer is ObjectSerializer builtInSerializer)
{
objectSerializer = builtInSerializer.WithAllowedTypes(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
}
There was a problem hiding this comment.
This is a good idea. I've made the change.
| _interfaceType = typeof(TInterface); | ||
| _discriminatorConvention = discriminatorConvention ?? BsonSerializer.LookupDiscriminatorConvention(typeof(TInterface)); | ||
| _objectSerializer = BsonSerializer.LookupSerializer<object>(); | ||
| _objectSerializer = objectSerializer ?? new ObjectSerializer(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type)); |
There was a problem hiding this comment.
It might be a problem if someone registered their own object serializer for some reasons. Can we implement method like a WithAllowedTypes on objectSerializer? So we lookup the same way as it was before - and then try to use this new method, similar to this:
var objectSerializer = BsonSerializer.LookupSerializer<object>();
if (objectSerializer is ObjectSerializer builtInSerializer)
{
objectSerializer = builtInSerializer.WithAllowedTypes(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
}
| else | ||
| _interfaceSerializer = interfaceSerializer; | ||
|
|
||
| if (objectSerializer == null) |
There was a problem hiding this comment.
If a user passes in an objectSerializer we should use it as-is with no further configuration on our part. It is up to the user to pass in a properly configured seiralizer.
…nts that interface in LINQ queries.
No description provided.